<template>
  <div class="index">

    <div class="center">
      <left></left>
      <div class="right">
        <top></top>
        <div class="hei10"></div>

        <div style="padding: 20px;">
          <div class="mb20" style=" font-size: 20px; margin-bottom: 30px;">
            <i class="el-icon-arrow-left" style="font-size: 20px; cursor: pointer; font-weight: bold;"
              @click="$router.back(-1)"></i>战队管理
          </div>
          <div class="kflexbc">
            <el-button @click="daka" style="margin-bottom: 20px;" round type="danger">新建战队</el-button>
          </div>

          <el-table :header-cell-style="{
                        background:'#F4F4F4',
                        color:'#7D8087'}" :data="tableData" stripe style="width: 100%">
            <el-table-column prop="name" label="战队名字">
            </el-table-column>
            <el-table-column prop="name" label="战队头像">
              <template slot-scope="scope">
                <img :src="scope.row.team_avatar" style="height: 100px;">
              </template>
            </el-table-column>
            <el-table-column prop="live_streamers_count" label="战队数量（人）">
            </el-table-column>
            <el-table-column prop="created_at" label="创建时间">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="text" @click="handleEdit( scope.row.id)">删除</el-button>
                <el-button type="text"
                  @click="bianji( scope.row)">编辑</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="kflex" style="justify-content: end; padding:20px 0;">
            <el-pagination background layout="total,prev, pager, next" :current-page="search.page" @current-change="fanye" :page-size="search.pagesize" :total="total"></el-pagination>
          </div>

        </div>
      </div>
    </div>
    <!-- <foot></foot> -->
    <!-- 打开弹窗 -->
    <el-dialog title="创建战队" :visible.sync="addzhubo" width="340px">
      <div style="text-align: center;">
        <div style="margin-bottom: 20px;"><el-input v-model="bianjiall.name" placeholder="输入战队"></el-input></div>
      </div>
      <div>
        <chuantu v-if="chuantushow" ref="chuantu" :imgs="bianjiall.team_avatar" @getimg="getimg"></chuantu>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button round @click="addzhubo = false">取消</el-button>
        <el-button type="danger" round @click="bianjigo">确定</el-button>
      </div>

    </el-dialog>

  </div>
</template>

<script>
  import axios from 'axios'
  import top from '@/components/top.vue'
  import left from '@/components/left.vue'
  import foot from '@/components/foot.vue'
  import chuantu from "../zujian/chuantu.vue";

  export default {
    components: {
      "left": left,
      "top": top,
      "foot": foot,
      chuantu
    },
    name: 'zhanduiguanli',
    data() {
      return {
        tableData: [],
        addzhubo: false,
        zhanduilist: [],
        bianjiall: {
          id:"",
          name:"",
          team_avatar:""
        },
        chuantushow: true,
        search:{
          page:1,
          page_size:20
        },
        total:0
      }
    },
    created() {
      this.shop_id = JSON.parse(localStorage.getItem('SHOP')).shop_id;
      this.getlist()
      axios.get('/api/live-stream/streamer/team/list').then((response) => {
        this.zhanduilist = response.data.data
      })
    },

    methods: {
      //翻页
      fanye:function(page){
      	this.search.page = page
      	this.getlist()
      },
      getimg: function(img) {
        this.bianjiall.team_avatar = img
      },
      bianji: function(item) {
        this.bianjiall = {
          id:item.id,
          name:item.name,
          team_avatar:item.team_avatar
        }
        this.addzhubo = true
        this.chuantushow = false
        this.$nextTick(()=>{
          this.chuantushow = true
        })
      },
      bianjigo: function() {
        if(!this.bianjiall.id){
          axios.post('/api/live-stream/term/createx', this.bianjiall).then((response) => {
            if (response.data.msg.code == 0) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              });
              this.addzhubo = false
              this.getlist()
            } else {
              this.$message.error(response.data.msg.info);
            }
          })
        }else{
          axios.put('/api/live-stream/term/edit', this.bianjiall).then((response) => {
            if (response.data.msg.code == 0) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              });
              this.addzhubo = false
              this.getlist()
            } else {
              this.$message.error(response.data.msg.info);
            }
          })
        }

      },
      getlist() {
        axios.get('/api/live-stream/team/list').then((response) => {
          if (response.data.msg.code == 0) {
            this.tableData = response.data.data.list
            this.total = response.data.data.count
          } else {
            this.$message.error(response.data.msg.info);
          }
        })
      },
      handleEdit(id) {
        this.$confirm('是否确认删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          axios.delete('/api/live-stream/term/delete?id='+id).then((response) => {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getlist()
          })
        })
      },

      daka() {
        this.bianjiall = {
          id:"",
          name:"",
          team_avatar:""
        }
        this.addzhubo = true
      },

    }
  }
</script>


<style lang="scss" scoped>
  .index {

    /deep/ .el-button--danger {
      background-color: #2974FF;
      border-color: #2974FF;
    }

    /deep/ .el-button--danger.is-plain {
      background-color: rgb(255, 255, 255);
      border-color: #2974FF;

      &:hover {
        color: #2974FF;
      }
    }
  }
</style>
